// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'element-ui/lib/index'
import http from './util/http';
import store from "./util/store";
import Moment from 'moment'


Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.prototype.moment = Moment

//在路由之前判断
//to：新路由， from:原路由 ，next:下一个
router.beforeEach((to,from,next)=>{
    //在页面title显示自定义文字
    document.title=to.meta.title

    if(to.fullPath==="/login" || to.fullPath==="/register"  || to.fullPath==="/shouye" || to.fullPath==="/product"  || to.fullPath==="/activity" || to.fullPath==="/shoppingCart" || to.fullPath==="/personalCenter" || to.fullPath==="/xq" || sessionStorage.getItem("token")){
        next(); // 向下访问新路由
    }else{
        console.log("未登录");
        next({path:"/"});
    }
})


//添加vue的属性
Vue.prototype.$http = http
/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>',
    store,
    watch:{
        /*$route:function (to,from) {
            console.log("跳转后的路径");
            console.log(to.path); //当前路径
            console.log("跳转前的路径");
            console.log(from.path); //前一个访问路径

            ///判断是否访问的是登录界面
            /!*if (to != "/"){
              let token = sessionStorage.getItem("token");
              if (!token){
                //替换
                this.$router.replace({name:'login'})
              }
            }*!/
        }*/
    }
})
